package com.yunxi.module.system.controller.admin.dict;

import com.yunxi.framework.apilog.core.annotation.ApiAccessLog;
import com.yunxi.framework.apilog.core.enums.OperateTypeEnum;
import com.yunxi.framework.common.pojo.CommonResult;
import com.yunxi.framework.common.pojo.PageParam;
import com.yunxi.framework.common.pojo.PageResult;
import com.yunxi.framework.common.util.object.BeanUtils;
import com.yunxi.framework.excel.util.ExcelUtils;
import com.yunxi.module.system.model.dict.DictTypeDO;
import com.yunxi.module.system.service.dict.DictTypeService;
import com.yunxi.module.system.vo.admin.dict.type.DictTypePageReqVO;
import com.yunxi.module.system.vo.admin.dict.type.DictTypeRespVO;
import com.yunxi.module.system.vo.admin.dict.type.DictTypeSaveReqVO;
import com.yunxi.module.system.vo.admin.dict.type.DictTypeSimpleRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 管理后台 - 字典类型
 * </p>
 *
 * @author lidy
 * @since 2024-06-05
 */
@Tag(name = "管理后台 - 字典类型")
@RestController
@RequestMapping("/system/dictType")
@Validated
public class DictTypeController {

    @Resource
    private DictTypeService dictTypeService;

    @GetMapping("/exportDictType")
    @Operation(summary = "导出字典类型")
    @PreAuthorize("@ss.hasPermission('system:dict:export')")
    @ApiAccessLog(operateType = OperateTypeEnum.EXPORT)
    public void exportDictType(HttpServletResponse response, @Valid DictTypePageReqVO reqVO) throws IOException {
        reqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictTypeDO> list = dictTypeService.getDictTypePage(reqVO).getList();
        ExcelUtils.write(response, "字典类型.xlsx", "字典类型",
                DictTypeRespVO.class, BeanUtils.toBean(list, DictTypeRespVO.class));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除字典类型")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    @Parameter(name = "typeId", description = "字典类型编号", required = true, example = "1024")
    public CommonResult<Boolean> deleteDictType(@RequestParam("typeId") Long typeId) {
        dictTypeService.deleteDictType(typeId);
        return CommonResult.success(true);
    }

    @PostMapping("/create")
    @Operation(summary = "创建字典类型")
    @PreAuthorize("@ss.hasPermission('system:dict:create')")
    public CommonResult<Long> createDictType(@Valid @RequestBody DictTypeSaveReqVO createReqVO) {
        Long typeId = dictTypeService.createDictType(createReqVO);
        return CommonResult.success(typeId);
    }

    @PutMapping("/update")
    @Operation(summary = "修改字典类型")
    @PreAuthorize("@ss.hasPermission('system:dict:update')")
    public CommonResult<Boolean> updateDictType(@Valid @RequestBody DictTypeSaveReqVO updateReqVO) {
        dictTypeService.updateDictType(updateReqVO);
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得字典详情")
    @Parameter(name = "typeId", description = "字典类型ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public CommonResult<DictTypeRespVO> getDictType(@RequestParam("typeId") Long typeId) {
        DictTypeDO dictTypeDO = dictTypeService.getDictType(typeId);
        return CommonResult.success(BeanUtils.toBean(dictTypeDO, DictTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得字典类型的分页列表")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public CommonResult<PageResult<DictTypeRespVO>> pageDictType(@Valid DictTypePageReqVO reqVO) {
        PageResult<DictTypeDO> pageResult = dictTypeService.getDictTypePage(reqVO);
        return CommonResult.success(BeanUtils.toBean(pageResult, DictTypeRespVO.class));
    }

    @GetMapping(value = {"/listAllSimple", "simpleList"})
    @Operation(summary = "获得全部字典类型列表", description = "包括 开启 + 禁用 的字典类型，主要用于前端的下拉选项")
    // 无需添加权限认证，因为前端全局都需要
    public CommonResult<List<DictTypeSimpleRespVO>> getSimpleDictTypeList() {
        List<DictTypeDO> list = dictTypeService.getDictTypeList();
        return CommonResult.success(BeanUtils.toBean(list, DictTypeSimpleRespVO.class));
    }

}
